import 'package:dmz/tools/blue/blue_business.dart';
import 'package:dmz/tools/blue/blue_config.dart';
import 'package:dmz/tools/blue/blue_manger.dart';
import 'package:dmz/tools/blue/blue_tools.dart';
import 'package:dmz/user/user_defurl.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
// import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:get/get.dart';
import '../../../base/controller/base_controller.dart';
import '../../../request/apis.dart';
import '../../../request/request_client.dart';
import '../../../user/user_model.dart';

class HomePageControllercs extends BaseController {
  final listData = RxList<ScanResult>([]);
  final contentNames = RxList<String>([]);
  RxString phone = ''.obs;
  RxString token = ''.obs;
  @override
  void loadNet() {}

  @override
  void onHidden() {}

  didBlueSan() {
    EMIBlueMangered().getSanDeviceList((p0) {
      listData.value = [];
      listData.addAll(p0);
      // update();
    });
  }

  login() {
    RequestClient().post<UserEntity>(APIS.login,
        data: {'password': 'Keren123456', 'phone': '13686334650', 'type': 0},
        onError: (p0) {
      print('sn验证4 -----${p0}');
      return false;
    }, onResponse: (p0) {
      print('sn验证3 -----${p0}');
    }).then((value) {
      token.value = value?.token ?? '';
      phone.value = value?.phone ?? '';
      EMIBUserManger().token = value?.token ?? '';
    });
  }

  loginOut() {
    EMIBUserManger().logout();
  }

  ///请求连接蓝牙
  checkBluetooth(ScanResult data) async {
    await EMIBlueMangered().stopScan();
    var snCode = EMIBlueTool.getSNcode(data);
    RequestClient().post<bool>(APIS.checkBluetooth, data: {'sn': snCode},
        onError: (p0) {
      return true;
    }, onResponse: (res) {
      if (res.code == 0 || res.code == 500) {
        didBindBluetooth(snCode, data);
        // EMIBlueManger().contentCallback(data, snCode, (success) {
        //   if (success) {
        //     EMIBUserManger().snCode = snCode;
        //     if (!contentNames.contains(data.device.advName)) {
        //       contentNames.add(data.device.advName);
        //     }
        //     print('连接成功 ${data.device.advName} $contentNames');
        //   } else {
        //     print('连接断开 ${data.device.advName} $contentNames');
        //     contentNames.remove(data.device.advName);
        //   }
        // });
      }
    });
  }

  didQQ(ScanResult data) {
    // EMIBlueMangered().contentQualifiedCharacteristic(data);
    EMIBlueMangered().disContenWidhSNCode(EMIBlueTool.getSNcode(data));
  }

  didBindBluetooth(snCode, ScanResult data) async {
    EMIBlueMangered().contentCallback(data, snCode, (success) {
      if (success) {
        EMIBUserManger().snCode = snCode;
        if (!contentNames.contains(data.device.advName)) {
          contentNames.add(data.device.advName);
        }
        print('连接成功 ${data.device.advName} $contentNames');
      } else {
        print('连接断开 ${data.device.advName} $contentNames');
        contentNames.remove(data.device.advName);
      }
    });
  }

  void sendBlueData(type) {
    EMIBlueBusiness.deviceOperate(EMIBUserManger().snCode!,
        type == 1 ? EMIBlueACT.setDeviceOpen : EMIBlueACT.setDeviceClose,
        callback: (String content) {
      print('蓝牙回调 ---- $content');
    });
  }
}

class HomeBindingcs extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut(() => HomePageControllercs());
  }
}
